package com.xinfu.newproject.presenter;

import com.xinfu.newproject.bean.meter.bean.AddUserMeterBean;
import com.xinfu.newproject.bean.meter.bean.CheckMeterCodeBean;
import com.xinfu.newproject.bean.meter.bean.MeterListBean;
import com.xinfu.newproject.bean.meter.body.AddUserMeterBody;
import com.xinfu.newproject.bean.meter.body.CheckMeterCodeBody;
import com.xinfu.newproject.bean.meter.body.MeterListBody;
import com.xinfu.newproject.callback.MyCallBack;
import com.xinfu.newproject.model.meter.MeterModel;
import com.xinfu.newproject.view.IView;

import io.reactivex.disposables.Disposable;

public class MeterPresenter {
    private IView.IMeterView mIMeterView;

    private MeterModel mMeterModel;

    public MeterPresenter(IView.IMeterView iMeterView) {
        this.mIMeterView = iMeterView;
        mMeterModel = new MeterModel();
    }

    /**
     * 手动添加水电表
     */
    public void addUserMeter() {
        mIMeterView.showProgress();
        AddUserMeterBody addUserMeterBody = new AddUserMeterBody(mIMeterView.header(), mIMeterView.userId(), mIMeterView.projectId(), mIMeterView.meterId());

        mMeterModel.addUserMeter(addUserMeterBody, new MyCallBack<AddUserMeterBean>() {
            @Override
            public void onSuccess(AddUserMeterBean addUserMeterBean) {
                mIMeterView.hideProgress();
                if (addUserMeterBean.isSuccess()) {
                    mIMeterView.getData(addUserMeterBean);
                } else {
                    mIMeterView.showToast(addUserMeterBean.getMessage());
                }
            }

            @Override
            public void onError(String header, String message) {
                mIMeterView.hideProgress();
                mIMeterView.showToast(message);
            }

            @Override
            public void onDispose(Disposable disposable) {
                mIMeterView.closeDispose(disposable);
            }
        });
    }

    /**
     * 校验meterCode
     */
    public void chechMeterCode() {
        mIMeterView.showProgress();
        CheckMeterCodeBody checkMeterCodeBody = new CheckMeterCodeBody(mIMeterView.header(), mIMeterView.meterCode(), mIMeterView.projectId(), mIMeterView.meterType());

        mMeterModel.checkMeterCode(checkMeterCodeBody, new MyCallBack<CheckMeterCodeBean>() {
            @Override
            public void onSuccess(CheckMeterCodeBean checkMeterCodeBean) {
                if (checkMeterCodeBean.isSuccess()) {
                    mIMeterView.getData(checkMeterCodeBean);
                } else {
                    mIMeterView.hideProgress();
                    mIMeterView.showToast(checkMeterCodeBean.getMessage());
                }
            }

            @Override
            public void onError(String header, String message) {
                mIMeterView.hideProgress();
                mIMeterView.showToast(message);
            }

            @Override
            public void onDispose(Disposable disposable) {
                mIMeterView.closeDispose(disposable);
            }
        });
    }

    /**
     * 校验meterCode
     */
    public void meterList() {
        mIMeterView.showProgress();
        MeterListBody meterListBody = new MeterListBody(mIMeterView.userId(), mIMeterView.meterType());

        mMeterModel.meterList(meterListBody, new MyCallBack<MeterListBean>() {
            @Override
            public void onSuccess(MeterListBean meterListBean) {
                mIMeterView.hideProgress();
                if (meterListBean.isSuccess()) {
                    mIMeterView.getData(meterListBean);
                } else {
                    mIMeterView.showToast(meterListBean.getMessage());
                }
            }

            @Override
            public void onError(String header, String message) {
                mIMeterView.hideProgress();
                mIMeterView.showToast(message);
            }

            @Override
            public void onDispose(Disposable disposable) {
                mIMeterView.closeDispose(disposable);
            }
        });
    }

    /**
     * 水电表支付
     */
    public void meterPay() {
        mIMeterView.showProgress();
        MeterListBody meterListBody = new MeterListBody(mIMeterView.userId(), mIMeterView.meterType());

        mMeterModel.meterPay(meterListBody, new MyCallBack<MeterListBean>() {
            @Override
            public void onSuccess(MeterListBean meterListBean) {
                mIMeterView.hideProgress();
                if (meterListBean.isSuccess()) {
                    mIMeterView.getData(meterListBean);
                } else {
                    mIMeterView.showToast(meterListBean.getMessage());
                }
            }

            @Override
            public void onError(String header, String message) {
                mIMeterView.hideProgress();
                mIMeterView.showToast(message);
            }

            @Override
            public void onDispose(Disposable disposable) {
                mIMeterView.closeDispose(disposable);
            }
        });
    }

}
